<template>
  <section class="f-order-sale">
    <div class="w-card my-content-box">
      <div class="title-box">
        <span class="title-text">商铺佣金</span>
      </div>
      <div class="my-search-box">
        <div>
          <span>商铺名称：</span>
          <el-input v-model="searchForm.shopName" clearable placeholder="请输入商铺名称" />
        </div>
        <div>
          <span>商铺类型：</span>
          <el-select v-model="searchForm.shopType" clearable>
            <el-option label="代理商" :value="1" />
            <el-option label="门店" :value="2" />
          </el-select>
        </div>
        <div class="datebox">
          <span>创建时间：</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" value-format="x" start-placeholder="开始时间"
            end-placeholder="结束时间" @change="(e) => {console.log(e)}" />
        </div>
        <div class="f-ml-20">
          <el-button @click="searchHandle" type="primary">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </div>
      </div>

      <div class="my-table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'12px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="80" label="序号" align="center" />
          <el-table-column prop="shopType" label="商铺类型" align="center">
            <template v-slot="scope">
              {{ scope.row.shopType == 1? '代理商': '门店' || '--' }}
            </template>
          </el-table-column>
          <el-table-column label="商铺名称" align="center">
            <template v-slot="scope">
              {{ scope.row.shopName|| '--' }}
            </template>
          </el-table-column>
          <el-table-column label="我的上级" align="center">
            <template v-slot="scope">
              {{ scope.row.agentShopName|| '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="operationalStatus" label="商铺状态" align="center">
            <template v-slot="scope">
              <p :style="{color: operationalStatusColor[scope.row.operationalStatus]}">
                {{ operationalStatus[scope.row.operationalStatus] }}
              </p>
            </template>
          </el-table-column>
          <el-table-column label="佣金总额" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeTotal|| '--' }}
            </template>
          </el-table-column>
          <el-table-column label="已冻结" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeFreezen|| '--' }}
            </template>
          </el-table-column>
          <el-table-column label="可提现" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeRestAvailable	|| '--' }}
            </template>
          </el-table-column>
          <el-table-column label="已申请" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeWithdrawWait|| '--' }}
            </template>
          </el-table-column>
          <el-table-column label="已提现" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeWithdrawUsed	|| '--' }}
            </template>
          </el-table-column>
          <el-table-column label="佣金余额" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeRestAvailable|| '--' }}
            </template>
          </el-table-column>
          <el-table-column label="实际到账" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeWithdrawArrive|| '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center">
            <template v-slot="scope">
              {{ getDateTime(scope.row.createTime)[6] || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="Operations" label="操作" align="center">
            <template v-slot="scope">
              <el-link v-if="scope.row.shopType != 1" type="primary" style="font-size:12px"
                @click="settingHandle(scope.row)">查看用户</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="my-table-footer">
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>

    <My-Detail ref="detailRef" />

    <router-view></router-view>
  </section>
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import MyDetail from './detail.vue'
import api from './api'

export default {
  components: { MyDetail },

  setup() {
    const route = useRoute()
    const router = useRouter()

    const detailRef = ref(null)

    const state = reactive({
      isDetail: false,

      searchForm: {
        "certificationStatus": "",
        "operationalStatus": "",
        "shopName": "",
      },

      tableHeight: 0,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,

      operationalStatus: {
        '0': '拉黑',
        '1': '正常',
        '2': '冻结',
      },
      operationalStatusColor: {
        '0': '#F56C6C',
        '1': '#2DB613',
        '2': '#E6A23C',
      },
    })

    watch(() => route.path, (newVal, oldVal) => {
      setIsDetail()
    })

    function setIsDetail() {
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        "certificationStatus": "",
        "operationalStatus": "",
        "shopName": "",
      }
      searchHandle()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        startTime: state.searchForm.date && state.searchForm.date[0],
        endTime: state.searchForm.date && state.searchForm.date[1],
        orderBy: 'id desc',
      }
      api.incomeList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.records
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function lookDetail(shopId) {
      router.push({ path: '/subordinate/detail', query: { shopId } })
    }

    function toAdd() {
      router.push({ path: '/subordinate/add' })
    }

    const shopExportAll = () => {
      const obj = {
        responseType: 'blob',
        ...state.searchForm,
        createTimeMin: state.searchForm.date && state.searchForm.date[0],
        createTimeMax: state.searchForm.date && state.searchForm.date[1],
      }
      api.shopExportAll(obj).then(res => {
        console.log(res)
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    //导入成功
    function importSuccess() {
      state.page = 1
      getTableData()
    }

    function settingHandle(data, type, name) {
      state.selectData = JSON.parse(JSON.stringify(data))
      detailRef.value.dialogShow(state.selectData)
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 290
      setIsDetail()
    })

    return {
      ...toRefs(state),
      detailRef,
      searchHandle,
      resetHandle,
      lookDetail,
      handleCurrentChange,
      handleSizeChange,
      getDateTime,
      shopExportAll,
      importSuccess,
      settingHandle,
      toAdd,
    }
  }
}
</script>

<style lang="scss" scoped>
.f-order-sale {
  .my-content-box {
    box-sizing: border-box;
    padding: 20px;

    > .title-box {
      display: flex;
      align-items: center;

      > .title-text {
        font-size: 16px;
        font-weight: bold;
      }
    }

    .my-search-box {
      box-sizing: border-box;
      padding: 20px 20px 20px 0;
      padding-bottom: 0;
      font-size: 14px;
      display: flex;
      align-items: center;
      gap: 10px;

      > div {
        width: 300px;
        display: flex;
        align-items: center;

        > span {
          flex-shrink: 0;
          width: 90px;
          text-align: right;
        }
      }

      .datebox {
        width: 450px;
      }
    }

    .my-table-box {
      margin-top: 20px;

      .table-handle-box {
        display: flex;
        align-items: center;
        gap: 0 20px;
      }

      .my-table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
        flex-wrap: wrap;
        gap: 10px;
      }
    }
  }
}
</style>